#include <glib.h>

/*
 * Use b[0 : n-1] record a[i]'s (0 <= i < n) 
 * maxlength with the end of each i
 */
static int max(int n, int b[])
{
	int temp = 0;
	int i = 0;

	for (i = 0; i < n; i++) {
		if (b[i] > temp) {
			temp = b[i];
		}
	}
	return temp;
}

int maxlen(int n, void *a)
{
	int i = 0;
	int j = 0;
	int k = 0;
	
	int *b = (int *)g_malloc0(sizeof(int) * n);
	if (NULL == b) {
		g_message("%s -- allocate error!", __func__);
		return -1;
	}
	for (i = 0, b[0] = 1; i < n; i++) {
		for (j = 0, k = 0; j < i; j++) {
			if (((int *)a)[j] < ((int *)a)[i] && k < b[j]) {
				k = b[j];
			}
		}
		b[i] = k + 1;
	}
	return max(n, b);
}
